﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.MODEL;
using WCSY.BLL;
using System.Data;

public partial class AuxiliaryManagement_BasicDataManagement_Initialbudget_InitialBu : System.Web.UI.Page
{
    Fg_deptInfoManager deptM = new Fg_deptInfoManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            if (Request["sysno"] != null)
            {
                //绑定预算编号
                txtsysno.Text = Request["sysno"].ToString();
                fg_InitialBuMstManager bum = new fg_InitialBuMstManager();
                fg_InitialBuMst mst = bum.GetMODEL(Request["sysno"].ToString());
                //绑定部门
                DataTable deptList = deptM.GetAllList().Tables[0];
                this.ddlDept.DataSource = deptList;
                this.ddlDept.DataTextField = "deptname";
                this.ddlDept.DataValueField = "id";
                this.ddlDept.DataBind();
                //绑定费用项
                fg_costsManager costsm = new fg_costsManager();
                this.ddlCosts.DataSource = costsm.GetAllList();
                this.ddlCosts.DataTextField = "name";
                this.ddlCosts.DataValueField = "id";
                this.ddlCosts.DataBind();
                //绑定期间
                this.ddlperiod.Items.Add("第一期");
                this.ddlperiod.Items.Add("第二期");
                this.ddlperiod.Items.Add("第三期");
                this.ddlperiod.Items.Add("第四期");
                this.ddlperiod.Items.Add("第五期");
                this.ddlperiod.Items.Add("第六期");
                this.ddlperiod.Items.Add("第七期");
                this.ddlperiod.Items.Add("第八期");
                this.ddlperiod.Items.Add("第九期");
                this.ddlperiod.Items.Add("第十期");
                this.ddlperiod.Items.Add("第十一期");
                this.ddlperiod.Items.Add("第十二期");
                ddlperiod.SelectedItem.Text = mst.period;
                //绑定年度
                this.txtUyear.Text = mst.uyear;
                //绑定预算表
                fg_InitialBuDetManager inim = new fg_InitialBuDetManager();
                this.GridView1.DataSource = inim.GetAllList();
                this.GridView1.DataBind();
                this.btnSave.Visible = false;
                tool.SetControlReadOnly(this);
            }
            else
            {
                //绑定预算编号
                txtsysno.Text = tool.GetMaxSysno("sysno", "fg_initialbuMst");
                //绑定部门
                DataTable deptList = deptM.GetAllList().Tables[0];
                this.ddlDept.DataSource = deptList;
                this.ddlDept.DataTextField = "deptname";
                this.ddlDept.DataValueField = "id";
                this.ddlDept.DataBind();
                //绑定费用项
                fg_costsManager costsm = new fg_costsManager();
                this.ddlCosts.DataSource = costsm.GetAllList();
                this.ddlCosts.DataTextField = "name";
                this.ddlCosts.DataValueField = "id";
                this.ddlCosts.DataBind();
                //绑定期间
                this.ddlperiod.Items.Add("第一期");
                this.ddlperiod.Items.Add("第二期");
                this.ddlperiod.Items.Add("第三期");
                this.ddlperiod.Items.Add("第四期");
                this.ddlperiod.Items.Add("第五期");
                this.ddlperiod.Items.Add("第六期");
                this.ddlperiod.Items.Add("第七期");
                this.ddlperiod.Items.Add("第八期");
                this.ddlperiod.Items.Add("第九期");
                this.ddlperiod.Items.Add("第十期");
                this.ddlperiod.Items.Add("第十一期");
                this.ddlperiod.Items.Add("第十二期");
                //绑定预算表
                fg_InitialBuMstManager inim = new fg_InitialBuMstManager();
                this.GridView1.DataSource = inim.getInitialList("0", "0");
                this.GridView1.DataBind();
                this.btnUpd.Visible = false;
                tool.SetControlReadOnly(txtsysno);
            }
        }
    }
    /// <summary>
    /// 根据条件查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSec_Click(object sender, EventArgs e)
    {
        fg_InitialBuMstManager inim = new fg_InitialBuMstManager();
        this.GridView1.DataSource = inim.getInitialList(ddlDept.SelectedValue, ddlCosts.SelectedValue);
        this.GridView1.DataBind();
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<string> sqlList = new List<string>();
        if (Request["sysno"] != null)
        {
            string sysno = Request["sysno"].ToString();
            #region 保存主表
            try
            {
                fg_InitialBuMstManager inim = new fg_InitialBuMstManager();
                fg_InitialBuMst mst = new fg_InitialBuMst();
                mst.sysno = sysno;
                mst.period = ddlperiod.SelectedItem.Text;
                mst.uyear = txtUyear.Text;
                string user = "";
                if (Session["userName"] != null)
                {
                    user = Session["userName"].ToString();
                }
                mst.fillpsn = user;
                mst.fillDate = DateTime.Now.Date;
                sqlList.Add(inim.Update(mst));
            }
            catch (Exception ex)
            {
                tool.Alert("主表修改失败！" + ex.Message, this);
                return;
            }
            #endregion
            #region 保存明细表
            try
            {
                
                fg_InitialBuDetManager detm = new fg_InitialBuDetManager();
                sqlList.Add(detm.Delete(Request["sysno"].ToString()));
                fg_InitialBuDet det = new fg_InitialBuDet();
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        det.sysno = sysno;
                        det.lineid = row.RowIndex + 1;
                        det.deptno = row.Cells[1].Text.ToString();
                        det.deptname = row.Cells[2].Text.ToString();
                        det.costsno = row.Cells[3].Text.ToString();
                        det.costsname = row.Cells[4].Text.ToString();
                        det.bumoney = Convert.ToDecimal((row.FindControl("txtBumoney") as TextBox).Text.Equals("") ? "0" :
                        (row.FindControl("txtBumoney") as TextBox).Text);
                        sqlList.Add(detm.Add(det));
                    }
                }

            }
            catch (Exception ex)
            {
                tool.Alert("明细表修改失败！" + ex.Message, this);
                return;
            }
            #endregion
            try
            {
                string suc = tool.executeSqlTran(sqlList);
                if (!suc.Equals("suc"))
                {
                    throw new Exception(suc);
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                error = error.Replace("\r\n", "");
                error = error.Replace("\n", "");
                error = error.Replace("\r", "");
                error = error.Replace("'", "");
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "", "alert('信息保存失败!" + error + "')", true);
                return;
            }
            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "", "alert('信息保存成功!');", true);
        }
        else
        {
            string sysno = tool.GetMaxSysno("sysno", "fg_initialbuMst");
            #region 保存主表
            try
            {
                fg_InitialBuMstManager inim = new fg_InitialBuMstManager();
                fg_InitialBuMst mst = new fg_InitialBuMst();
                mst.sysno = sysno;
                mst.period = ddlperiod.SelectedItem.Text;
                mst.uyear = txtUyear.Text;
                string user = "";
                if (Session["userName"] != null)
                {
                    user = Session["userName"].ToString();
                }
                mst.fillpsn = user;
                mst.fillDate = DateTime.Now.Date;
                sqlList.Add(inim.Add(mst));
            }
            catch (Exception ex)
            {
                tool.Alert("主表保存失败！" + ex.Message, this);
                return;
            }
            #endregion
            #region 保存明细表
            try
            {
                fg_InitialBuDetManager detm = new fg_InitialBuDetManager();
                fg_InitialBuDet det = new fg_InitialBuDet();
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        det.sysno = sysno;
                        det.lineid = row.RowIndex + 1;
                        det.deptno = row.Cells[1].Text.ToString();
                        det.deptname = row.Cells[2].Text.ToString();
                        det.costsno = row.Cells[3].Text.ToString();
                        det.costsname = row.Cells[4].Text.ToString();
                        det.bumoney = Convert.ToDecimal((row.FindControl("txtBumoney") as TextBox).Text.Equals("") ? "0" :
                        (row.FindControl("txtBumoney") as TextBox).Text);
                        sqlList.Add(detm.Add(det));
                    }
                }

            }
            catch (Exception ex)
            {
                tool.Alert("明细表保存失败！" + ex.Message, this);
                return;
            }
            #endregion
            try
            {
                string suc = tool.executeSqlTran(sqlList);
                if (!suc.Equals("suc"))
                {
                    throw new Exception(suc);
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                error = error.Replace("\r\n", "");
                error = error.Replace("\n", "");
                error = error.Replace("\r", "");
                error = error.Replace("'", "");
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "", "alert('信息保存失败!" + error + "')", true);
                return;
            }
            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "", "alert('信息保存成功!');", true);
        }

    }
    /// <summary>
    /// 预算部门选择时，查询该部门费用项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSec_Click(sender, e);
    }
    /// <summary>
    /// 预算费用选择时，查询该预算费用所有部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlCosts_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSec_Click(sender, e);
    }
    //点击修改按钮，取消所有控件只读，并隐藏修改按钮
    protected void btnUpd_Click(object sender, EventArgs e)
    {
        tool.SetControlReadOnlyNone(this);
        tool.SetControlReadOnly(ddlDept);
        tool.SetControlReadOnly(ddlCosts);
        tool.SetControlReadOnly(txtsysno);
        btnUpd.Visible = false;
        btnSave.Visible = true;
    }
}
